import type { FormItemRule } from 'element-plus'

export type FormValidator = (
  rule: FormItemRule,
  value: number | string | string[] | number[],
  callback: (err?: string | Error) => void
) => void
export const validateHandler = <T = string | number | string[] | number[]>(
  value: T,
  handler: (data: string) => string | undefined
): string | undefined => {
  if (!Array.isArray(value)) {
    return handler((value as string | number).toString())
  }
  let errMsg: string | undefined
  value.find(item => {
    errMsg = handler(item)
    if (errMsg) {
      return true
    }
    return false
  })
  return errMsg
}
/**正则-域名 */
// export const domainReg =
// /^(?=^.{3,255}$)[a-zA-Z0-9][-a-zA-Z0-9]{0,62}(\.[a-zA-Z0-9][-a-zA-Z0-9]{0,62})+$/
/**正则-域名 包含泛域名 */
export const wildcardDomainReg =
  /^(?=^.{3,255}$)([a-zA-Z0-9]+(-[a-zA-Z0-9]+)*\.)+[a-zA-Z]{2,11}$/

/**正则-域名 */
export const domainReg =
  /^(?=^.{3,253}$)(?!((.*((\-\-)|(\.\-)|(\-\.))[^\.])|(^\-.*)))([a-zA-Z0-9\-_]{1,63}?\.)+[a-zA-Z0-9]([a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])?$/

/**正则-域名每一级 */
export const domainSplitReg = /^[a-zA-Z0-9\-_]{1,63}$/

/**正则-IPv4 */
export const ipv4Reg =
  /^((1?[1-9]?\d|[1-2][0-4]\d|25[0-5])\.){3}(1?[1-9]?\d|[1-2][0-4]\d|25[0-5])$/

/**正则-IPv6 */
export const ipv6Reg =
  /(^(?:(?:(?:[0-9A-Fa-f]{1,4}:){7}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){6}:[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){5}:([0-9A-Fa-f]{1,4}:)?[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){4}:([0-9A-Fa-f]{1,4}:){0,2}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){3}:([0-9A-Fa-f]{1,4}:){0,3}[0-9A-Fa-f]{1,4})|(([0-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5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b)\.){3}(\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b))|(::([0-9A-Fa-f]{1,4}:){0,5}((\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b)\.){3}(\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b))|([0-9A-Fa-f]{1,4}::([0-9A-Fa-f]{1,4}:){0,5}[0-9A-Fa-f]{1,4})|(::([0-9A-Fa-f]{1,4}:){0,6}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){1,7}:))\](?::(?:[0-9]|[1-9][0-9]{1,3}|[1-5][0-9]{4}|6[0-4][0-9]{3}|65[0-4][0-9]{2}|655[0-2][0-9]|6553[0-5]))?$)/
// url地址
export const urlReg =
  /^http(s)?:\/\/(((([a-zA-Z\d]+-?)+(?<!-)\.)+[a-z]{2,11})|(localhost)|((1?[1-9]?\d|[1-2][0-4]\d|25[0-5])\.){3}(1?[1-9]?\d|[1-2][0-4]\d|25[0-5]))(:([1-9][0-9]{0,3}|[1-5][0-9]{4}|6[0-4][0-9]{3}|65[0-4][0-9]{2}|655[0-2][0-9]|6553[0-5]))?(\/\S*)?$/
/**正则-邮箱 */
export const emailReg =
  /^(?=^.{3,200}$)\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/
// 正则通用验证器
export const regExpValidateHandler = (
  value: string | number,
  reg: RegExp,
  errMsg: string
) => {
  if (!reg.test(value.toString())) {
    return errMsg
  }
  return undefined
}

// 验证泛域名，包括正常域名
export const validatePanDomain: FormValidator = (
  _rule,
  value: any,
  callback
) => {
  callback(
    validateHandler(value, data => {
      const error = '域名格式错误'
      if (data.startsWith('*.')) {
        data = data.substring(2, data.length - 1)
      }
      return regExpValidateHandler(data, wildcardDomainReg, error)
    })
  )
}
// 验证url
export const validateUrl: FormValidator = (_rule, value: any, callback) => {
  callback(
    validateHandler(value, data => {
      const error = '请输入以http/https开头完整的地址'
      return regExpValidateHandler(data, urlReg, error)
    })
  )
}
